package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utils.DBUtility;
import com.utils.GenerateTime;

public class TestAccountUpdateAccoutAndPwdServlet extends HttpServlet {

	private Connection conn;
	private PreparedStatement pst;
	private ResultSet rs;
	/**
	 * 
	 */
	private static final long serialVersionUID = 9047780228252969401L;



	/**
	 * Constructor of the object.
	 */
	public TestAccountUpdateAccoutAndPwdServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		String  account_name = request.getParameter( "account" );
		String account_pwd = request.getParameter( "pwd" );
		String phoneMIEI  = request.getParameter( "phoneMIEI" );


		System.out.println("account :" + account_name );

		PrintWriter out = response.getWriter();

		String info = "";
		boolean flag = false;
		flag = this.checkPhoneMIEI(phoneMIEI);

		if(  ! flag  ){
			int i = this.addNewAccountToDB(account_name, account_pwd);
			
			if( i > 0 ){
				info = "add-succesed";
				System.out.println("[TestAccountUpdateServer]new temp account add successfully");
				
				addPhoneMIEIToDB( phoneMIEI );
			}else{
				
				info ="add-failed";
			} 

		}else{
			info ="add-failed-num-exist";
		}
		out.print( info );
		out.flush();
		out.close();

	}

	/**
	 * @param miei
	 */
	private void addPhoneMIEIToDB( String miei ){
		conn = DBUtility.getMySqlConnection();
		
		String sql = "insert into db.jiankong_phone_miei_table( phone_miei ) values(?)";
		
		try {
			pst = conn.prepareStatement( sql );
			
			pst.setString(1 ,miei );
			
			int i  = pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			closeAllConnections();
			
		}
	}


	/**
	 * @param account
	 * @param pwd
	 * @return
	 */
	private int addNewAccountToDB( String account, String pwd ){
		int i = 0;
		String add_time = GenerateTime.getCurrentTime();
		String end_time = GenerateTime.getTempAccountEndTime();

		String sql = "INSERT INTO db.jiankong_account_table (account_use_type, account_type, userid, end_time, belongwho, add_time, PASSWORD, used_flag)VALUES('avilable', 'temp', ?, ?, 'admin', ?, ?, 'no')";
		System.out.println("[TestAccountUpdateServer]add_time:"+ add_time );
		System.out.println("[TestAccountUpdateServer]end_time:"+ end_time );
		conn = DBUtility.getMySqlConnection();

		try {
			pst = conn.prepareStatement( sql );

			pst.setString( 1, account);
			pst.setString( 2, end_time);
			pst.setString( 3, add_time);
			pst.setString( 4, pwd);

			i = pst.executeUpdate();


		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

			closeAllConnections();

		}
		System.out.println( "[TestAccountUpdateServer]insert sql :" +  "INSERT INTO db.jiankong_account_table (account_use_type, account_type, userid, end_time, belongwho, add_time, PASSWORD, used_flag)VALUES('avilable', 'temp', '"+ account +"', '"+end_time + "', 'admin', '"+add_time+"', '"+pwd+"', 'no')");
		return i;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 */
	private void closeAllConnections(){

		if( rs != null ){

			try {
				rs.close();

				rs = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if( pst != null){

			try {
				pst.close();

				pst = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}



	}

	/**
	 * @param phoneMIEI
	 * @return
	 */
	private boolean checkPhoneMIEI( String phoneMIEI ){
		boolean flag = false;

		String sql = "SELECT phone_miei	FROM db.jiankong_phone_miei_table where phone_miei = ?";

		conn = DBUtility.getMySqlConnection();
		
		try {
			pst = conn.prepareStatement( sql );
			pst.setString(1,  phoneMIEI );
			rs = pst.executeQuery();

			if( rs.next() ){

				flag = true;

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{

			this.closeAllConnections();

		}

		return flag;

	} 


}
